package com.wisnews.controller.sr;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.wisnews.pojo.Input;
import com.wisnews.pojo.Result;
import com.wisnews.pojo.sr.entity.Comment;
import com.wisnews.service.sr.Impl.CommentServiceImpl;
import com.wisnews.util.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yuyd
 * @since 2021-01-15
 */
@RestController
@RequestMapping("/comment")
@Slf4j
public class CommentController {

    @Autowired
    private CommentServiceImpl commentService;

    @RequestMapping(value = "/addComment/{newsId}",method = RequestMethod.POST)
    public Result addComment(@RequestBody Map<String,String> desc,
                             @PathVariable(value = "newsId") String newsId){

        String context = desc.get("desc");

        commentService.saveComment(newsId,context);
        return new Result(Constants.SYS_CODE.SUCCESS, Constants.SYS_CODE.SUCCESS_MESSAGE);
    }

    @RequestMapping(value = "/getComment4NewsId/{newsId}",method = RequestMethod.POST)
    public Result<IPage> getComment4NewsId(@PathVariable(value = "newsId") String newsId, Input input ){

        if (StringUtils.isBlank(newsId)){
            return new Result<>(Constants.SYS_CODE.FAIL, Constants.SYS_CODE.FAIL_MESSAGE);
        }

        IPage<Comment> commentIPage = commentService.getComment4NewsId(newsId,input.toPage());

        return new Result(Constants.SYS_CODE.SUCCESS, Constants.SYS_CODE.SUCCESS_MESSAGE,commentIPage);

    }

    @RequestMapping(value = "getCommentInfo",method = RequestMethod.POST)
    public Result<IPage> getCommentInfo(@RequestBody Input<Comment> input){

        IPage<Comment> commentList = commentService.queryCommentList(input.getReq(),input.toPage());

        return new Result(Constants.SYS_CODE.SUCCESS, Constants.SYS_CODE.SUCCESS_MESSAGE,commentList);
    }

}
